﻿// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using Admin.NET.Application.Entity;
using Admin.NET.Core.Service;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;

/// <summary>
/// 用户信息服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class KerryplususerinfoService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<Kerryplususerinfo> _kerryplususerinfoRep;
    public KerryplususerinfoService(SqlSugarRepository<Kerryplususerinfo> kerryplususerinfoRep)
    {
        _kerryplususerinfoRep = kerryplususerinfoRep;
    }

    /// <summary>
    /// 分页查询用户信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    [DisplayName("分页查询用户信息")]
    public async Task<SqlSugarPagedList<KerryplususerinfoOutput>> Page(PageKerryplususerinfoInput input)
    {
		input.SearchKey = input.SearchKey?.Trim();
        var query = _kerryplususerinfoRep.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(input.SearchKey), u =>
                u.UnionId.Contains(input.SearchKey)
                || u.OpenId.Contains(input.SearchKey)
                || u.AppId.Contains(input.SearchKey)
                || u.BrandId.Contains(input.SearchKey)
                || u.LbsId.Contains(input.SearchKey)
                || u.UserId.Contains(input.SearchKey)
                || u.PhoneNumber.Contains(input.SearchKey)
                || u.NickName.Contains(input.SearchKey)
                || u.VipCode.Contains(input.SearchKey)
                || u.Token.Contains(input.SearchKey)
                || u.RefreshToken.Contains(input.SearchKey)
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.UnionId), u => u.UnionId.Contains(input.UnionId.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.OpenId), u => u.OpenId.Contains(input.OpenId.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.AppId), u => u.AppId.Contains(input.AppId.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.BrandId), u => u.BrandId.Contains(input.BrandId.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.LbsId), u => u.LbsId.Contains(input.LbsId.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.UserId), u => u.UserId.Contains(input.UserId.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.PhoneNumber), u => u.PhoneNumber.Contains(input.PhoneNumber.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.NickName), u => u.NickName.Contains(input.NickName.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.VipCode), u => u.VipCode.Contains(input.VipCode.Trim()))
            .WhereIF(input.State>0, u => u.State == input.State)
            .WhereIF(input.LastTimeRange != null && input.LastTimeRange.Length == 2, u => u.LastTime >= input.LastTimeRange[0] && u.LastTime <= input.LastTimeRange[1])
            .WhereIF(!string.IsNullOrWhiteSpace(input.Token), u => u.Token.Contains(input.Token.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.RefreshToken), u => u.RefreshToken.Contains(input.RefreshToken.Trim()))
            .WhereIF(input.TokenUpdateTimeRange != null && input.TokenUpdateTimeRange.Length == 2, u => u.TokenUpdateTime >= input.TokenUpdateTimeRange[0] && u.TokenUpdateTime <= input.TokenUpdateTimeRange[1])
            .WhereIF(input.LastSignDateTimeRange != null && input.LastSignDateTimeRange.Length == 2, u => u.LastSignDateTime >= input.LastSignDateTimeRange[0] && u.LastSignDateTime <= input.LastSignDateTimeRange[1])
            .WhereIF(input.NextSignDateTimeRange != null && input.NextSignDateTimeRange.Length == 2, u => u.NextSignDateTime >= input.NextSignDateTimeRange[0] && u.NextSignDateTime <= input.NextSignDateTimeRange[1])
            .Select<KerryplususerinfoOutput>();
		return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加用户信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    [DisplayName("增加用户信息")]
    public async Task<long> Add(AddKerryplususerinfoInput input)
    {
        var entity = input.Adapt<Kerryplususerinfo>();
        await _kerryplususerinfoRep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除用户信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    [DisplayName("删除用户信息")]
    public async Task Delete(DeleteKerryplususerinfoInput input)
    {
        var entity = await _kerryplususerinfoRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _kerryplususerinfoRep.FakeDeleteAsync(entity);   //假删除
        //await _kerryplususerinfoRep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新用户信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    [DisplayName("更新用户信息")]
    public async Task Update(UpdateKerryplususerinfoInput input)
    {
        var entity = input.Adapt<Kerryplususerinfo>();
        await _kerryplususerinfoRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取用户信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    [DisplayName("获取用户信息")]
    public async Task<Kerryplususerinfo> Detail([FromQuery] QueryByIdKerryplususerinfoInput input)
    {
        return await _kerryplususerinfoRep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取用户信息列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    [DisplayName("获取用户信息列表")]
    public async Task<List<KerryplususerinfoOutput>> List([FromQuery] PageKerryplususerinfoInput input)
    {
        return await _kerryplususerinfoRep.AsQueryable().Select<KerryplususerinfoOutput>().ToListAsync();
    }





}
